package com.example.front.arrange;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

/**
 * StartTask
 *
 * @author liming2
 * @date 2023/11/7 11:49
 */
public abstract class AbstractRootTask<R> extends AbstractTask<R> {

    private List<AbstractTask<?>> subTaskList = new ArrayList<>();


    public AbstractRootTask() {
        this.setParents(Collections.emptyList());
    }

    protected abstract R startParam();

    @Override
    protected R handle() {
        R param = startParam();
        this.setResult(param);
        return param;
    }

    public void setSubTaskList(Collection<AbstractTask<?>> subTaskList) {
        this.subTaskList.addAll(subTaskList);
    }

    public List<AbstractTask<?>> getSubTaskList() {
        return subTaskList;
    }


    public <T> T getResultOfTask(Class<? extends AbstractTask<T>> taskClass) {
        for (AbstractTask<?> task : subTaskList) {
            if (task.getClass() == taskClass || task.getClass().getSuperclass() == taskClass) {
                return (T) task.getResult();
            }
        }
        return null;
    }
}
